import com.bigdata.entity.User;
import java.io.File;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
//import example.avro.*;
public class TestProduct {

    public static void main(String[] args) throws  Exception{
        //序列化
        User user=User.newBuilder().setCompanyName("aa").setDirection("dfd").setProductId("1").setProductInfo("好嗨啊").build();
        User user1=User.newBuilder().setCompanyName("aa2").setDirection("dfd2").setProductId("2").setProductInfo("好嗨啊2").build();
        File file=new File("d:\\users1231.avro");  //生成文件存放的位置
        DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
        DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
        dataFileWriter.create(user.getSchema(),file);
        dataFileWriter.append(user);
        dataFileWriter.append(user1);
        dataFileWriter.close();
        //反序列化
        DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
        DataFileReader<User> dataFileReader = new DataFileReader<User>(file, userDatumReader);
        User user2= null;
        while (dataFileReader.hasNext()) {
            user2 = dataFileReader.next();
            System.out.println(user);
        }

    }
}
